<?php
/**
 * @Created by PhpStorm.
 * @Name 资金流水
 * @User: djh/Wong
 * @Date: 2016/11/11
 * @Copyright : MPE.Fiidee.Monda.Cn
 */

namespace member\action;

use api\utils\PayApiClient;
use herosphp\http\HttpRequest;
use herosphp\utils\AjaxResult;
use herosphp\core\Log;


class FundAction  extends CommonAction{

    protected $dasu_id=null;
    protected $signInfo=null;

    public function __construct(){
        parent::__construct();
        $this->dasu_id = $this -> _user -> __get('dasu_pay_id');
        $this->signInfo = $this->_user->getSignInfo();

    }

    /**
     * 流水类型名称
     * @param null $k
     * @return array|mixed
     */
    public function logTypes($k=null){
        $arr = array(
            'inpay' => '入金',
            'outpay' => '出金',
            'cash' => '提现',
            'pay' => '支付',
            'confirm' => '确认收货'
        );
        return $k?$arr[$k]:$arr;
    }

    /**
     * 资金总览
     */
    public function index(){
        $total_cost = $this->_user->__get('total_cost');
        $usable = $this->_user->__get('usable');
        $payway = $this->_user->__get('payway');

        $data['userid'] = $this->dasu_id;
        $url = "api/payment/getBalance";
        $result = PayApiClient::send($url, $data);
        if($result->isSucess()){
            $res = $result->getData();
            $balance = $res['balance'];
        }else{
            Log::info($result->getMessage());
            $balance = 0;

        }
        $balance = setFloat($balance,2);
        //5条资金流水
        $conditions = array();
        $conditions['userid'] = $this->dasu_id;
        $data['conditions'] = $conditions;
        $data['order'] = array('addtime' => '-1');
        $data['page'] = '1';
        $data['pagesize'] = '5';
        $url = "api/userLog/getUserLogs";
        $result = PayApiClient::send($url, $data);
        if ( $result->isSucess() ) {
            $items = $result->getItems();
        } else {
            Log::info($result->getMessage());
            $items = array();
        }
        foreach($items as $k=>$v){
            $items[$k]['type_name'] = $this->logTypes($v['type']);
        }
        $this->assign('items',$items);
        $this->assign('total_cost',$total_cost);
        $this->assign('usable',$usable);
        $this->assign('payway',$payway);
        $this->assign('balance',$balance);
        $this->setView("/fund/fund_index");
    }

    /**
     * 获取用户资金流水
     * @param HttpRequest $request
     */
    public function log(HttpRequest $request){
        $type = $request->getParameter('type');
        $month = $request->getParameter('month');
        $s_time = $request->getParameter('s_time');
        $e_time = $request->getParameter('e_time');
        $page = $request->getParameter('page');
        if($page<=0) $page = 1;
        $pagesize = 8;
        $this->setPage($page);
        $this->setPagesize($pagesize);

        $conditions = array();
        $conditions['userid'] = $this->dasu_id;
        if($type) $conditions['type'] = $type;
        if($s_time) $conditions['addtime']['>'] = $s_time;
        if($e_time) $conditions['addtime']['<='] = $e_time.' 23:59:59';
        switch($month){
            case 1:
                $conditions['addtime']['>'] = date('Y-m-d H:i:s',time()-24*60*60*30);
                break;
            case 3:
                $conditions['addtime']['>'] = date('Y-m-d H:i:s',time()-24*60*60*30*3);
                break;
            case 12:
                $conditions['addtime']['>'] = date('Y-m-d H:i:s',time()-24*60*60*30*12);
                break;
            default:
                break;
        }
        $data['conditions'] = $conditions;
        $data['order'] = array('addtime' => '-1');
        $data['page'] = (string)$page;
        $data['pagesize'] = (string)$pagesize;
        $url = "api/userLog/getUserLogs";
        $result = PayApiClient::send($url, $data);
        if ( $result->isSucess() ) {
            $items = $result->getItems();
            $total = $result->getCount();
        } else {
            Log::info($result->getMessage());
            $items = array();
        }
        foreach($items as $k=>$v){
            $items[$k]['type_name'] = $this->logTypes($v['type']);
        }
        $this->memberPagemenu($total);

        $this->assign('items',$items);
        $this->assign('month',$month);
        $this->assign('s_time',$s_time);
        $this->assign('e_time',$e_time);
        $this->assign('type',$type);
        $requestType = $request->getParameter('ajax');
        if($requestType) {
            $this->setView('/fund/fund_log_page');
        }else {
            $this->setView('/fund/fund_log');
        }
    }

    /**
     * 入金
     * @param HttpRequest $request
     */
    public function inPay(HttpRequest $request){
        $data['userid'] = $this->dasu_id;
        $data['amount'] = $request->getParameter('amount','trim');
        $token = $request->getParameter('token','trim');

        if($_SESSION['payToken'] != $token){
            AjaxResult::ajaxResult('001','参数错误');
        }
        $url = "api/payment/inPay";

        $result = PayApiClient::send($url, $data);
        if($result->isSucess()){
            $this->_user->updateSignInfo();//重新抓取saas数据
            AjaxResult::ajaxSuccessResult();
        }else{
            Log::info($result->getMessage());
        }
    }

    /**
     * 出金
     * @param HttpRequest $request
     */
    public function outPay(HttpRequest $request){
        $data['userid'] = $this->dasu_id;
        $data['amount'] = $request->getParameter('amount','trim');
        $token = $request->getParameter('token','trim');

        if($_SESSION['payToken'] != $token){
            AjaxResult::ajaxResult('001','参数错误');
        }

        $url = "api/payment/outPay";

        $result = PayApiClient::send($url, $data);

        if($result->isSucess()){
            $this->_user->updateSignInfo();//重新抓取saas数据
            AjaxResult::ajaxSuccessResult();

        }else{
            Log::info($result->getMessage());
            AjaxResult::ajaxFailtureResult();
        }
    }

    /**
     * 提现
     * @param HttpRequest $request
     */
    public function withdraw(HttpRequest $request){
        $data['userid'] = $this->dasu_id;
        $data['amount'] = $request->getParameter('amount','trim');
        $token = $request->getParameter('token','trim');

        if($_SESSION['payToken'] != $token){
            AjaxResult::ajaxResult('001','参数错误');
        }

        $url = "api/payment/withdraw";

        $result = PayApiClient::send($url, $data);
        if($result->isSucess()){
            $this->_user->updateSignInfo();//重新抓取saas数据
            AjaxResult::ajaxSuccessResult();

        }else{
            Log::info($result->getMessage());
            AjaxResult::ajaxFailtureResult();
        }
    }

    /**
     * 查询附属账户余额
     * @param HttpRequest $request
     */
    public function getBalance(){
        $this->_user->updateSignInfo();//重新抓取saas数据
        $data['userid'] = $this->dasu_id;
        $url = "api/payment/getBalance";
        $result = PayApiClient::send($url, $data);
        if($result->isSucess()){
            $res = $result->getData();
            $balance = $res['balance'];
        }else{
            Log::info($result->getMessage());
            $balance = 0;
        }
        $balance = setFloat($balance,2);
        AjaxResult::ajaxResult('000','用户余额',$balance);
    }

    /**
     * 检测用户是否签约 是否设置交易密码
     */
    public function isSign(){
        $this->_user->updateSignInfo();//重新抓取saas数据
        $signInfo = $this->signInfo;
        if($signInfo['tradepass'] == ''){
            AjaxResult::ajaxResult('001','您还未设置交易密码，请至“账户安全”内设置交易密码！');
        }
        if ($signInfo['sign_status'] == '2'){//解约
            AjaxResult::ajaxResult('001','已经解约');
        }elseif($signInfo['sign_status'] == '0'){//签约
            AjaxResult::ajaxResult('001','没有签约');
        }else{
            AjaxResult::ajaxSuccessResult();
        }
    }

    /**
     * 返回签约的银行信息
     */
    public function bankInfo(){
        $this->_user->updateSignInfo();//重新抓取saas数据
        $signInfo = $this->signInfo;
        $data = array(
            'bankType'=>$signInfo['payway'],
            'virtualAccount'=>$signInfo['virtual_account'],
            'bankId'=>$signInfo['sign_bank_id'],
        );
        AjaxResult::ajaxResult('000','ok',$data);
    }
}